<?php
    include_once("../action/checkAuthenticationAction.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Danh sách hóa đơn</title>
	<?php 
        include("import-css.php");
        include("../util/constant.php");
        include("../util/message.php");
		include_once("../util/authUtil.php");
   		$perm_array = $_SESSION["permissions"];
    ?>
</head>
<body>
	<?php include("header.php");?>
<form id="form" method="post">
	<div class="container-fluid">
		<div class="row-fluid">
			<?php 
                include("menu.php");
                include_once("../service/receiptService.php");
            ?>
			<div id="content" class="span10">
                <div id="msg" class="center alert"></div>
                <div class="row-fluid sortable">
    				<div class="box span12">
    					<div class="box-content">
    						<table class="table table-striped table-bordered bootstrap-datatable datatable">
                                <thead>
                                    <tr>
                                        <th>STT</th>
                                        <th>Mã</th>
                                        <th>Ngày</th>
                                        <th>Trạng thái</th>
                                        <th>Chức năng</th>                                        
                                    </tr>
    							 </thead>   
    							 <tbody>
                                 	<?php
										$receipts = findAllReceipt();
										$count = 1;
										while($row =  ConnectionManager::getInstance()->fetch($receipts)){
                                    ?>
                                            <tr>
                                                <td><?php echo $count++;?></td>
                                                <td><?php echo $row["REC_CODE"];?></td>
                                                <td>
													<?php 
														$date = new DateTime($row["REC_DATE"]);
									                    echo $date->format('d/m/Y');
													?>
                                                </td>
                                                <td>
                                                	<select name="status" <?php if(authUtil(array("edit receipt"),$perm_array,true)){?>onChange="updateStatus(<?php echo $row["REC_ID"];?>, this.value);"<?php }?>>
                                                	<?php
														$statuses = findAllStatus();
														while($status = current($statuses)){
															if(key($statuses) == $row["REC_STATUS"]){
                                                    ?>
                                                    			<option value="<?php echo key($statuses); ?>" selected="selected"><?php echo($status);?></option>
                                                    <?php
															} else {
													?>
                                                    			<option value="<?php echo key($statuses); ?>"><?php echo($status);?></option>
                                                    <?php
															}
															next($statuses);
														}
                                                    ?>
                                                    </select>
                                                </td>
                                                <td>
                                                	<div class="modal hide fade" id="receiptModal<?php echo $row["REC_ID"];?>"></div>
                                                    <a href="#receiptModal<?php echo $row["REC_ID"];?>" role="button" class="btn btn-success" data-toggle="modal" onClick="getReceiptInformation(<?php echo $row["REC_ID"];?>);">
                                                        <i class="icon-zoom-in icon-white"></i>  
                                                        Chi tiết
                                                    </a>
                                                    <?php
														$require_perm = array("edit receipt");
														if(authUtil($require_perm,$perm_array,true)){
													?>
                                                    <a class="btn btn-info" href="receipt-detail.php?action=<?php echo EDIT_ACTION; ?>&id=<?php echo $row["REC_ID"];?>">
                                                        <i class="icon-edit icon-white"></i>  
                                                        Chỉnh sửa                                            
                                                    </a>
                                                    <?php
														}
                                                    ?>
                                                </td>                                      
                                            </tr>
                                    <?php
										}
                                    ?>                
    							 </tbody>
    						 </table>     
    					</div>
    				</div><!--/span-->
    			</div><!--/row-->
            </div><!-- end of content -->
        </div><!-- end of row-fluid -->
        <?php include("footer.php");?>
    </div><!-- container-fluid -->
    <?php include("import-js.php");?>
    <script>
		function updateStatus(recId, status){
			 var dataString = "recId=" + recId + "&status="+ status;
            
            $.ajax({  
                type: "POST",  
                url: "../action/ajax/updateStatusReceiptProcess.php",  
                data: dataString,
                async: false,  
                success: function(data) {
                    if(data == <?php echo UPDATE_SUCCESSFULLY;?>){
                        showMessage("<?php echo $messages["editStatusReceiptSuccessfully"];?>", "success");
                    } else{
                        showMessage("<?php echo $messages["editStatusReceiptFailed"];?>", "error");
                    }
                }
            });
		}
		
		function  getReceiptInformation(recId){
            var dataString = "recId=" + recId;
            
            $.ajax({
                type: "POST",
                url: "../action/ajax/getReceiptInformationProcess.php",
                data: dataString,
                success: function(data){
                    $("#receiptModal" + recId).html(data);
                }
            });
        }
    </script>
</form>
</body>
</html>